﻿Imports System.Data.OleDb

Public Class EditClub
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim clubID As String = Request.Params("ClubID")

        If Not IsPostBack Then
            If Not String.IsNullOrEmpty(clubID) Then
                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
                oleDbConn.Open()

                Dim selectClubSQL As String = "SELECT * FROM Clubs WHERE ID=@f1"
                Dim selectClubCmd As OleDbCommand = New OleDbCommand(selectClubSQL, oleDbConn)
                selectClubCmd.CommandType = CommandType.Text
                selectClubCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))

                Dim selectClubDr As OleDbDataReader = selectClubCmd.ExecuteReader()

                If selectClubDr.HasRows Then
                    selectClubDr.Read()

                    If Not IsDBNull(selectClubDr("ClubName")) Then
                        lblClub.Text = selectClubDr("ClubName")
                        txtClubName.Text = selectClubDr("ClubName")
                    End If

                    If Not IsDBNull(selectClubDr("FoundationDate")) Then txtFoundationDate.Text = selectClubDr("FoundationDate")
                    If Not IsDBNull(selectClubDr("Address")) Then txtClubAddress.Text = selectClubDr("Address")
                    If Not IsDBNull(selectClubDr("CountryID_FK")) Then ddlCountry.SelectedValue = selectClubDr("CountryID_FK")
                Else
                    Session("UserMessage_Error") = "The specified Club does not exist!"
                    Response.Redirect("~/ClubList.aspx")
                End If

                selectClubCmd.Dispose()
                oleDbConn.Close()
                oleDbConn.Dispose()
            Else
                Session("UserMessage_Error") = "A Club must be specified!"
                Response.Redirect("~/ClubList.aspx")
            End If
        End If

    End Sub

    Protected Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
        If Page.IsValid Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid

            Dim newFileName = myGUID.ToString + ".jpg"

            If fuEmblem.HasFile Then
                Dim picLocationOnServerHardDisk = Request.MapPath("Emblems") & "/" & newFileName
                fuEmblem.SaveAs(picLocationOnServerHardDisk)
            End If

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            Dim updateClubSQL As String

            If fuEmblem.HasFile Then
                updateClubSQL = "UPDATE Clubs SET ClubName=@f1,FoundationDate=@f2,Address=@f3,CountryID_FK=@f4,Emblem=@f5,Username=@f6,UpdatedAt=@f7 WHERE ID=@f8"
            Else
                updateClubSQL = "UPDATE Clubs SET ClubName=@f1,FoundationDate=@f2,Address=@f3,CountryID_FK=@f4,Username=@f6,UpdatedAt=@f7 WHERE ID=@f8"
            End If

            Dim updateClubCmd As OleDbCommand = New OleDbCommand(updateClubSQL, oleDbConn)
            updateClubCmd.CommandType = CommandType.Text
            updateClubCmd.Parameters.AddWithValue("@f1", txtClubName.Text)
            updateClubCmd.Parameters.AddWithValue("@f2", txtFoundationDate.Text)
            updateClubCmd.Parameters.AddWithValue("@f3", txtClubAddress.Text)
            updateClubCmd.Parameters.AddWithValue("@f4", ddlCountry.SelectedValue)

            If fuEmblem.HasFile Then
                updateClubCmd.Parameters.AddWithValue("@f5", "Emblems/" & newFileName)
            End If

            updateClubCmd.Parameters.AddWithValue("@f6", User.Identity.Name)
            updateClubCmd.Parameters.AddWithValue("@f7", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
            updateClubCmd.Parameters.AddWithValue("@f8", Request.Params("ClubID"))

            oleDbConn.Open()
            updateClubCmd.ExecuteNonQuery()
            System.Threading.Thread.Sleep(500)
            Session("UserMessage") = "The club was successfully updated!"
            Response.Redirect("~/ClubDetail.aspx?ClubID=" + Request.Params("ClubID"))

            updateClubCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

    Protected Sub btnBackClub_Click(sender As Object, e As EventArgs) Handles btnBackClub.Click
        Response.Redirect("~/ClubDetail.aspx?ClubID=" + Request.Params("ClubID"))
    End Sub
End Class